package com.droidnova.android.games.vortex;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import android.view.MotionEvent;
	//public class VortexRenderer implements GLSurfaceView.Renderer {
		public class VortexRenderer	extends GLSurfaceView implements Renderer
		{
		//private float _red = 0.9f;
	   // private float _green = 0.2f;
	   // private float _blue = 0.2f;
		private float _red = 0.01f;
			   private float _green = 0.01f;
			    private float _blue = 0.02f;	
	   /** axis instance */
		private axis axis3d;
		/** points instance */
		private points pointcube;
		
		private lines linecube;
		/** Angle For The axis */
		private float raxis; 	
		/** Angle For The points */
		private float rpoints; 
		int width , height;
		/* Rotation values */
		private float xrot;					//X Rotation
		private float yrot;					//Y Rotation

		/* Rotation speed values */
		private float xspeed;				//X Rotation Speed
		private float yspeed;				//Y Rotation Speed
		/* Variables and factor for the input handler */
		private float oldX;
	    private float oldY;
		private final float TOUCH_SCALE = 0.2f;			//Proved to be good for normal rotation
		/** The Activity Context */
		private Context context;
		/**
		 * Instance the Pyramid and Cube objects
		 */
		
		public VortexRenderer(Context context) {
			super(context);
			
			//Set this as Renderer
			this.setRenderer(this);
			//Request focus, otherwise buttons won't react
			this.requestFocus();
			this.setFocusableInTouchMode(true);
			
			//
			this.context = context;	
			float a,b,c;
			axis3d = new axis();
			
			
			
			pointcube = new points(context);
			
			//a=pointcube.getMax_xvalue();
			//b=pointcube.getMax_xvalue();
			//c=pointcube.getMax_xvalue();
			//linecube = new lines(context,a,b,c);
		}
		 @Override
		 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		   //Really Nice Perspective Calculations
				gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
		 }
		 
		    @Override
		    public void onSurfaceChanged(GL10 gl, int w, int h) {
		        width = w;
		        height = h;
		        Log.v("Width Height", "w h -> " + w + h);
		    	if(h == 0) { 						//Prevent A Divide By Zero By
					h = 1;                          //Making Height Equal One  
		    	}
		    	gl.glViewport(0, 0, w, h);         //Reset The Current Viewport
		    	
		    }
	    
	
	    
		@Override
	    public void onDrawFrame(GL10 gl ) {
	    	
	    	float aspect_ratio = (float) width / height;
	    	GLU.gluPerspective(gl, 70, aspect_ratio, 1, 100);
	    	// first clear the screen of any previous drawing
	        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	        gl.glClearColor(_red, _green, _blue, 1.0f);
	     
	        // clear the color buffer to show the ClearColor we called above...
	        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	        
	        gl.glLoadIdentity();
	        
	       // gl.glPushMatrix();
	     // set rotation
	        
	       // gl.glRotatef(raxis, 1.0f, 1.0f, 0.0f);
	        // set the color of our element
	        gl.glColor4f(0.5f, 0f, 0.5f, 0.5f);
	        
	        axis3d.draw(gl);
	        
	     // Restore the last matrix.
	    	//gl.glPopMatrix();
	    	// gl.glPushMatrix();
	        gl.glLoadIdentity();   
	        //gl.glRotatef(_angle, 0.5f, 0f, 0.5f);
	       // gl.glRotatef(rpoints, 1.0f, 1.0f, 1.0f);
	        // set the color of our element
	        gl.glColor4f(0.5f, 0f, 0.5f, 0.5f);
	        //gl.glTranslatef(-1f,0.0f,-6.0f); 
	       // gl.glTranslatef(0, 0, -4); 
	       // gl.glTranslatef(1.0f,1.0f,0.0f); 
	      //  gl.glScalef(0.9f, 0.9f, 0.9f);      // Scale down (NEW)
	        
	        
	       // gl.glTranslatef(-1f,0.0f,-6.0f); 
	       // gl.glTranslatef(0.0f, 1.3f, -6.0f);		//Move up 1.3 Units and -6.0 into the Screen
	        //gl.glScalef(0.8f, 0.8f, 0.8f); 	
			
	       // gl.glRotatef(rtri, 1.0f, 1.0f, 1.0f);
	        //square.draw(gl);
	        //cube.draw(gl);
	       // triangle.draw(gl);
	      //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
			gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);	//X
			gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);	//Y
	        pointcube.draw(gl);
	        
	      //  gl.glLoadIdentity();  
	      //  gl.glColor4f(0.5f, 0f, 0.5f, 0.5f);
	      //  gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);	//X
			//gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);	//Y
			//linecube.draw(gl);
			
			
			
			
	     // Restore the last matrix.
	    	//gl.glPopMatrix();
	     // Update the rotational angle after each refresh (NEW)
	       // angleLine += speedLine;   // (NEW)
	       // angleQuad += speedQuad;         // (NEW)
	    	raxis += 0.2f;
	    	//rpoints += 0.2f;
	    	
	    	//Change rotation factors
			xrot += xspeed;
			yrot += yspeed;
	    }
	    
	    
	    /**
		 * Override the touch screen listener.
		 * 
		 * React to moves and presses on the touchscreen.
		 */
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			//
			float x = event.getX();
	        float y = event.getY();
	        
	        //If a touch is moved on the screen
	        if(event.getAction() == MotionEvent.ACTION_MOVE) {
	        	//Calculate the change
	        	float dx = x - oldX;
		        float dy = y - oldY;

	        	//Rotate around the axis otherwise
	        	        		
	    	        xrot += dy * TOUCH_SCALE;
	    	        yrot += dx * TOUCH_SCALE;
	        	}        

	        //Remember the values
	        oldX = x;
	        oldY = y;
	        
	        //We handled the event
			return true;
		}
	
	}
	
	
	

